/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q2507.java
 * @Author : WangRuoyu
 * @Date 2022/12/23 17:44
 */

package leetcode;

public class Q2507 {
    public int smallestValue(int n) {
        int res = n;

        int x = n;
        while (true) {
            int tmp = 0;
            int y = x;
            for (int i = 2; i <= x / i; i++) {
                if ((x % i) == 0) {
                    while ((x % i) == 0) {
                        x /= i;
                        tmp += i;
                    }
                }
            }

            if (x == y) {
                res = x;
                break;
            } else if (x != 1) {
                tmp += x;
            }
            x = tmp;
            if (x == n) {
                return n;
            }
        }

        return res;
    }

    public static void main(String[] args) {
        Q2507 sl = new Q2507();
        int n = 4;
        System.out.println(sl.smallestValue(n));
    }
}